#ifndef V4L2CAMERA_H
#define V4L2CAMERA_H

#include <QThread>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <QImage>
#include <errno.h>
#include <stdio.h>

typedef struct
{
    unsigned char*start;
    size_t len;
}buffer_t;
class v4l2camera : public QThread
{
    Q_OBJECT
public:
    explicit v4l2camera(QObject *parent = nullptr);
    ~v4l2camera();
    void run();
    int v4l2_init();
    int convert_yuv_to_rgb_pixel(int y, int u, int v);
    int convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
public slots:
    void stop_signal();
signals:
    void sendimage(QImage image);
    void thread_complete();
private:
     QString video_device = "/dev/video0";
    int camerafd;
    buffer_t buffer[4];
    buffer_t currentstart;
    unsigned char rgb_buffer[640*480*3];
    unsigned int maxlength;
    unsigned int stopflag;
    struct v4l2_requestbuffers requestbuffers;

};

#endif // V4L2CAMERA_H
